﻿Imports MySql.Data.MySqlClient
Public Class CGuru
    Inherits CContext

    Private INSERT_COMMAND As String
    Private UPDATE_COMMAND As String
    Private DELETE_COMMAND As String
    Private SELECT_COMMAND As String

    Public Sub New()
        MyBase.New()
    End Sub

    Public Function getById(idguru As Integer) As tbl_guru
        Dim _res As New tbl_guru
        MyBase.MySqlConnect.Open()
        Dim SELECT_CMD As MySqlCommand = MyBase.MySqlConnect.CreateCommand
        Dim reader As MySqlDataReader
        With SELECT_CMD
            .CommandText = "SELECT G.ID, G.NAMA, G.NIP FROM TBL_GURU G WHERE G.ID = " & idguru
            reader = .ExecuteReader
        End With
        While reader.Read
            _res.id = reader.Item("id")
            _res.nama = reader.Item("nama")
            _res.nip = reader.Item("nip")
        End While
        reader.Close()
        MySqlConnect.Close()
        Return _res
    End Function

    Public Sub simpan(ByVal nip As String, ByVal nama As String)
        Me.INSERT_COMMAND = "INSERT INTO TBL_GURU (NIP,NAMA) VALUES ('" & nip & "','" & nama & "')"
        MyBase.InsertData(INSERT_COMMAND)
    End Sub

    Public Sub rubah(ByVal nip As String, ByVal nama As String, ByVal idGuru As Integer)
        Me.UPDATE_COMMAND = "UPDATE TBL_GURU SET NAMA='" & nama & "', NIP='" & nip & "' WHERE ID=" & idGuru
        MyBase.UpdateData(UPDATE_COMMAND)
    End Sub

    Public Sub hapus(ByVal idGuru As Integer)
        'validasi
        If idGuru > 0 Then
            Me.DELETE_COMMAND = "DELETE FROM TBL_GURU WHERE ID=" & idGuru
            MyBase.DeleteData(DELETE_COMMAND)
        Else
            Throw New Exception
        End If
    End Sub

    Public Function getAll() As DataTable
        Me.SELECT_COMMAND = "SELECT * FROM TBL_GURU"
        Return MyBase.SelectData(SELECT_COMMAND)
    End Function

    Public Function getAllExceptIn(ByVal exceptIdArray As Integer())
        Me.SELECT_COMMAND = "SELECT * FROM TBL_GURU"
        Return MyBase.SelectData(SELECT_COMMAND)
    End Function

End Class
